package com.shycloud.mido.music.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.vo.AppAppShoppingCartVO;
import com.shycloud.mido.music.domain.MisLibColl;
import com.shycloud.mido.music.domain.MisMusic;
import com.shycloud.mido.music.domain.MisMusicCollection;
import com.shycloud.mido.music.dto.MisMusicCollectionAddDTO;
import com.shycloud.mido.music.dto.MisMusicCollectionDTO;
import com.shycloud.mido.music.feign.MemberFeignService;
import com.shycloud.mido.music.mapper.MisLibCollMapper;
import com.shycloud.mido.music.mapper.MisMusicCollectionMapper;
import com.shycloud.mido.music.mapper.MisMusicMapper;
import com.shycloud.mido.music.mapper.MisUserCollectionMapper;
import com.shycloud.mido.music.service.MisMusicCollectionService;
import com.shycloud.mido.music.util.CommonString;
import com.shycloud.mido.music.vo.MyCollectionVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 曲集模块 service 实现类
 *
 * @author nianhua.jiang
 * @date 2020/5/6 15:12
 */
@Service
@AllArgsConstructor
public class MisMusicCollectionServiceImpl extends ServiceImpl<MisMusicCollectionMapper, MisMusicCollection> implements MisMusicCollectionService {

	/** 曲集模块 mapper */
	private MisMusicCollectionMapper misMusicCollectionMapper;
	/** 曲库曲集关系表 mapper */
	private MisLibCollMapper misLibCollMapper;
	/** 曲目模块 mapper */
	private MisMusicMapper misMusicMapper;
	/** 收藏曲集模块 mapper */
	private MisUserCollectionMapper userCollectionMapper;
	/** member feign service */
	private MemberFeignService memberFeignService;

	/**
	 * 分页获取曲集信息（移动端）
	 *
	 * @param page
	 * @param libraryId
	 * @return com.baomidou.mybatisplus.core.metadata.IPage
	 * @author nianhua.jiang
	 * @date 2020/5/6 15:13
	 */
	@Override
	public IPage getMusicConllectionMobile(Page page, Integer libraryId, Integer chargeType) {

		if (SecurityUtils.getMember() == null) {
			return misMusicCollectionMapper.getMusicConllectionMobile(page, libraryId, "xxx", chargeType);
		}

		return misMusicCollectionMapper.getMusicConllectionMobile(page, libraryId, SecurityUtils.getMember().getId(), chargeType);
	}

	@Override
	public IPage getMusicConllection(Page page, Integer libraryId, String CollectionName, Integer collectionId, String collectionStatus) {

		IPage<MisMusicCollectionDTO> musicConllection = misMusicCollectionMapper.getMusicConllection(page, libraryId, CollectionName, collectionId, collectionStatus);

		//循环统计曲目解析失败的数量
		List<MisMusicCollectionDTO> misMusicCollectionDTOS = musicConllection.getRecords();

		for (MisMusicCollectionDTO misMusicCollection : misMusicCollectionDTOS) {
			Integer failCount = misMusicMapper.selectCount(new QueryWrapper<MisMusic>()
					.eq("collection_id", misMusicCollection.getId())
					.eq("status", "0")
					.eq("del_flag","0")
					.ne("file_status", "0")
					.ne("file_status", "1"));
			misMusicCollection.setMusicFailCount(failCount);
		}

		musicConllection.setRecords(misMusicCollectionDTOS);

		return musicConllection;

	}

	/**
	 * 添加曲集
	 *
	 * @param misMusicCollectionDto 添加曲集DTO
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/5/6 17:30
	 */
	@Override
	public boolean saveCollection(MisMusicCollectionAddDTO misMusicCollectionDto) {

		MisMusicCollection misMusicCollection = new MisMusicCollection();
		BeanUtil.copyProperties(misMusicCollectionDto, misMusicCollection);
		//执行插入/更新操作
		if (super.saveOrUpdate(misMusicCollection)) {
			//获得曲库Id的list集合
			List<Integer> libraryIdList = misMusicCollectionDto.getLibraryId();
			if (libraryIdList != null && libraryIdList.size() > 0) {
				//无论新增还是修改都先清空【曲库-曲集】关系表
				misLibCollMapper.delete(new QueryWrapper<MisLibColl>().eq(MisLibColl.COL_CONLLECTION_ID, misMusicCollectionDto.getId()));
				//添加【曲库-曲集】关系表
				libraryIdList.forEach(x -> {
					//初始化【曲库曲集关系表】
					MisLibColl misLibColl = new MisLibColl();
					//曲库ID
					misLibColl.setLibraryId(x);
					//曲集ID
					misLibColl.setConllectionId(misMusicCollection.getId());
					//逻辑删除
					misLibColl.setDelFlag(CommonString.DELETE_FLG);
					//新增
					misLibCollMapper.insert(misLibColl);
				});
			}
		}
		return true;
	}

	/**
	 * 删除曲集
	 *
	 * @param collectionId
	 * @return int
	 * @author nianhua.jiang
	 * @date 2020/5/7 13:12
	 */
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public int deleteMusicConllection(Integer collectionId) {

		//1.删除曲库-曲集关系表
		QueryWrapper<MisLibColl> queryWrapperLibColl = new QueryWrapper<>();
		//曲集ID
		queryWrapperLibColl.eq(MisLibColl.COL_CONLLECTION_ID, collectionId);
		//查询需要删除的数据
		List<MisLibColl> misLibColls = misLibCollMapper.selectList(queryWrapperLibColl);
		for (MisLibColl misLibColl : misLibColls) {
			//逻辑删除状态改为 1->已删除
			misLibColl.setDelFlag(CommonString.DELETE_FLG_DEL);
			//执行
			misLibCollMapper.updateById(misLibColl);
		}

		//2.删除曲集下的所有曲目
		List<MisMusic> misMusics = misMusicMapper.selectList(new QueryWrapper<MisMusic>().eq(MisMusic.COL_COLLECTION_ID, collectionId));
		for (MisMusic misMusic : misMusics) {
			//逻辑删除状态改为 1->已删除
			misMusic.setDelFlag(CommonString.DELETE_FLG_DEL);
			//执行
			misMusicMapper.updateById(misMusic);
		}

		//3.删除曲集
		MisMusicCollection misMusicCollection = misMusicCollectionMapper.selectById(collectionId);
		//逻辑删除状态改为 1->已删除
		misMusicCollection.setDelFlag(CommonString.DELETE_FLG_DEL);
		//执行
		int conllectionResult = misMusicCollectionMapper.updateById(misMusicCollection);

		return conllectionResult;
	}


	/**
	 * 获取我收藏的有声图书
	 *
	 * @param page
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.shycloud.mido.music.vo.MyCollectionVO>
	 * @author nianhua.jiang
	 * @date 2020/6/16 16:32
	 */
	@Override
	public IPage<MyCollectionVO> getMySoundBook(Page page) {

		//获取member_id
		String memberId = SecurityUtils.getMember().getId();

		return userCollectionMapper.getMySoundBook(page, memberId);

	}

	@Override
	public MisMusicCollection getValidCollectionById(Integer collectionId) {
		return misMusicCollectionMapper.getValidCollectionById(collectionId);
	}
}
